package com.inspur.dgov.bsp.user.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.inspur.dgov.bsp.user.entity.PubRegion;
import com.inspur.dgov.bsp.user.service.PubRegionService;
import com.inspur.dgov.bsp.user.vo.RegionWebVO;
import com.inspur.dgov.common.tool.api.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 开放接口
 *
 * @Author chiyupei
 * @Date 20/3/20 下午6:19
 * @Version 1.0
 * @Description 开放接口
 */
@RestController
@AllArgsConstructor
@RequestMapping("/web")
@Api(value = "开放接口", tags = "开放接口")
public class WebController {

    private PubRegionService pubRegionService;

    /**
     * 区划接口
     */
    @GetMapping("/region")
    @ApiOperation(value = "区划服务", notes = "传入上级区划ID", position = 1)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "regionId", value = "上级区划ID", paramType = "query", dataType = "String")
    })
    public R<List<RegionWebVO>> region(@RequestParam(value = "regionId", required = false) String regionId) {
        List<RegionWebVO> regionList = new ArrayList<>();
        QueryWrapper<PubRegion> queryWrapper = null;
        PubRegion root;
        if (StrUtil.isBlank(regionId)) {
            //获取最根节点
            queryWrapper = new QueryWrapper<>();
            queryWrapper.isNull("parent_id");
            root = this.pubRegionService.getOne(queryWrapper);
        } else {
            root = new PubRegion();
            root.setId(Long.valueOf(regionId));
        }
        if (null != root && null != root.getId()) {
            regionList = this.pubRegionService.webRegion(root);
        }
        return R.data(regionList);
    }
}
